<?php 
	$token = md5(rand().time());
	// Start Retrieve network List
	$sqlNetworkAds = "select * from ".NETWORKMASTER.",".NETWORKDETAILS." where ".NETWORKMASTER.".status='Y' AND ".NETWORKMASTER.".networkId=".NETWORKDETAILS.".networkId";
	$execNetworkAds = mysql_query($sqlNetworkAds);
	$execNetworkAds1 = mysql_query($sqlNetworkAds);
	// End Retrieve network List

	$optionstring='';
	while($fetNetworkAds1 = mysql_fetch_array($execNetworkAds1))
	{
		$temp=$fetNetworkAds1[networkId]."-".$fetNetworkAds1[networkType];
		$optionstring .= '<option value='.$temp.'>'.ucfirst($fetNetworkAds1['networkName']).' '.'$'.$fetNetworkAds1['networkMinPrice'].'</option>';

	}

?>
<form name="create_campaign" method="POST" id="create_campaign" action=""  enctype="multipart/form-data">
<table border="0" width="100%">
	<!-- <tr>
		<td>
		<table>
			<tr>
				<td>Ad Groups What's this?</td>
			</tr>
			<tr>
				<td id="no_of_adGroups">Ad Groups: 1</td>
			</tr>
			<tr>
				<td id="no_of_ads">Ads: 0</td>
			</tr>
			<tr>
				<td><input type="button" name="create_ad_group"
					value="Create Ad Group" class='group-create' /></td>
			</tr> 
		</table>
		</td>
	</tr>-->
	<tr>
			<td colspan="2">
			<div style="width:100%">
			<?php if(!empty($message)){ echo $message; } ?>
			</div>
			</td>
		</tr>
	<tr>
		<td><input name="help_insert" id="help_insert" value="help_insert" type="hidden">
			<table width="100%" border="0" class="table-group">
				<tbody>
					<tr class='a-group'>
						<td>
							<table width="100%" name="createad" id="createad">
								<tr>
									<td>
										<table width="100%" border="0" cellspacing="0" cellpadding="0">
								<tr>
									<td style="padding: 0px; margin: 0px;" class="txt-top">
									<table width="100%" border="0" cellspacing="0" cellpadding="0">
										<tr>
											<td width="11" style="padding: 0px; margin: 0px;"><img
												src="images/PinkBorderTopLeft.jpg" alt="" width="11" /></td>
											<td class="PinkBoxTopBg" style="padding: 0px; margin: 0px;"
												class="txt-top"><img src="images/spacer.gif" alt=""
												width="10" /></td>
											<td width="11" style="padding: 0px; margin: 0px;"><img
												src="images/PinkBorderTopRight.jpg" alt="" width="11" /></td>
										</tr>
									</table>
									</td>
								</tr>
								<tr>
									<td class="PinkTwosideBorder">
									<table width="100%" border="0" align="center" cellpadding="0"
										cellspacing="0">
										<tr>
											<td>
											<table width="100%">
												<tr>
													<td width="23">&nbsp;</td>
													<td width="435" id="ad_type_var"><strong>Ad Type:</strong>
													<span class="ad-type-value">undefined</span></td>
													<td width="749"><strong>Units:</strong> <span
														class="unit-value">undefined</span></td>
												</tr>
												<tr>
													<td>&nbsp;</td>
													<td><strong>Ad Group Name</strong></td>
													<td><input type="text" name="add_gr_name"
														id="add_gr_name"></td>
												</tr>
												<tr>
													<td></td>
													<td></td>
													<td id="validate_add_gr_name" style="display: none;"></td>
												</tr>
												<tr>
													<td>&nbsp;</td>
													<td><strong>Daily Budget</strong></td>
													<td><input type="text" name="daily_budget"
														id="daily_budget"></td>
												</tr>
												<!-- Custom Query -->
												<?php
												$query_check = "select * from temp_create_network where identity = 'main_adgroup' ";
												$res_check = mysql_query($query_check);
												$row_check = mysql_fetch_array($res_check);
												?>
												<!-- End of custom Query -->
												<tr>
													<td>&nbsp;</td>
													<td><strong>Network</strong></td>
													<td style="width: 1em;" id="adgroup_network"><select
														name="network" id="network" class="select-network"
														style="width: 190px;">
														<option value="">Select Network</option>
														<option value="custom">Custom</option>
														<option value="mobile">Mobile</option>
														<?
														while($fetNetworkAds = mysql_fetch_array($execNetworkAds)){
															?>
														<option
															value="<?=$fetNetworkAds[networkId]."-".$fetNetworkAds[networkType]."-".$fetNetworkAds["networkBidType"]?>"><?php echo ucfirst($fetNetworkAds[networkName]); ?>&nbsp;$<?php echo $fetNetworkAds[networkMinPrice]; ?>
														</option>
														<? } ?>
													</select></td>
												</tr>
												<!--Country list for iPhone-->
												<?php
												$sql_country = "select countryId, countryName from country where status='Y'";
												$res_country = mysql_query($sql_country);
												?>
												<tr id="rCountry" class="rCountry" style="display: none;">
													<td>&nbsp;</td>
													<td><strong>Country</strong></td>
													<td style="width: 1em;" id="adgroup_country"><select
														name="country" id="country" style="width: 190px;">
														<?
														while($fetCountryAds = mysql_fetch_array($res_country))
														{ ?>
														<option value="<?php echo $fetCountryAds[countryId]?>"><?php echo $fetCountryAds[countryName]; ?></option>
														<? } ?>
													</select></td>
												</tr>
												<tr>
													<td></td>
													<td></td>
													<td id="validate_network" style="display: none;"></td>
												</tr>
												<tr>
													<td></td>
													<td></td>
													<td id="main_adgroup_save" style="display: none;"></td>
												</tr>
												<tr>
													<td id="tdCreateAdcreatead" colspan="3" align="center"></td>
												</tr>
												<!-- 
												<tr>
													<td></td>
													<td></td>
													<td><input type="button" name="create_ad" class='create-ad'
														id="create_ad" value="Create Ad"></td>
												</tr> -->
												<tr id="show_campaign_network" style="display: none">
													<td colspan="3" align="center">Please Select The Network</td>
												</tr>
											</table>
											</td>
										</tr>
										<!-- 
										<tr id="tdCreateAd">
											<td>&nbsp;</td>
										</tr> -->
										<tr class="g-remove" style="display: none">
											<td align="right"><a href="javascript:void(0);"
												class="group-remove">Remove</a>&nbsp;&nbsp;&nbsp;</td>
										</tr>
									</table>
									</td>
								</tr>

								<tr>
									<td class="txt-top">
									<table width="100%" border="0" cellspacing="0" cellpadding="0">
										<tr>
											<td width="11"><img src="images/PinkBorderBottomLeft.jpg"
												alt="" width="11" height="11" /></td>
											<td class="PinkBoxBottomBg"><img src="images/spacer.gif"
												alt="" width="10" height="11" /></td>
											<td width="11"><img src="images/PinkBorderBottomRight.jpg"
												alt="" width="11" height="11" /></td>
										</tr>
									</table>
									</td>
								</tr>
							</table>
							</td>
						</tr>
					</table>
					</td>
				</tr>

			</tbody>
		</table>

		</td>
	</tr>
	
</table>

<table align="center">
	<tr>
		<td><input type="hidden" name="token" value="<?php echo $token;?>" />
		<input type="submit" class='validateSubmit' name="submit" value="Next" />
		<input type="hidden" name="mode" value="step2" />
		<input type="hidden" name="step" value="3" />
		<input type="submit" name="cancel" value="Cancel" /></td>
	</tr>
</table>
</form>
<script language="javascript">
		function IsNumeric(strString)
		//  check for valid numeric strings
		{
			var strValidChars = "0123456789";
			var strChar;
			var blnResult = true;
			
			if (strString.length == 0) return false;
			
			//  test strString consists of valid characters listed above
			for (i = 0; i < strString.length && blnResult == true; i++)
			{
				strChar = strString.charAt(i);
				if (strValidChars.indexOf(strChar) == -1)
				{
					blnResult = false;
				}
			}
			return blnResult;
		}
		jQuery('document').ready(function(){
			
			//delete a group
			jQuery('a.group-remove').die('click');
			jQuery('a.group-remove').live('click',function(){
				totalGroup =jQuery('tr.a-group').size();	
				if(totalGroup>1)	//remove when greater than 1 group
					jQuery(this).parents('tr.a-group').remove();
				refresh_groups();
			});
			//refresh status of groups
			function refresh_groups()
			{
				totalGroup =jQuery('tr.a-group').size();				
				if(totalGroup>1)
					jQuery('a.group-remove').parents('tr.g-remove').css('display','');
				else
					jQuery('a.group-remove').parents('tr.g-remove').css('display','none');
				//update total group
				jQuery('#no_of_adGroups').text('Ad Groups: '+totalGroup);
					
			}
			//create group
			jQuery('.group-create').click(function(){
				aboveGroup = jQuery('tr.a-group:last');
				newGroup = jQuery('tr.a-group:first').clone();
				newGroup.find('input[type=text]').val('');
				newGroup.find('.rCountry').hide();
				aboveGroup.after(newGroup);				
				//refresh
				refresh_groups();
			});
			//change network type
			jQuery('select.select-network').die('change');
			jQuery('select.select-network').live('change',function(){
				val = jQuery(this).val();
				if(val.toLowerCase()=='custom')
				{
					GB_showCenter('Create Custom network','../createbuildCustomNetwork.php?form_id=main_adgroup',500,800);
				}else if(val.toLowerCase() == 'mobile')
				{
					currentGroup = jQuery(this).parents('tr.a-group');
					currentGroup.find('.rCountry').show();
					//GB_showCenter('Create Mobile','../advertiser_direct_mobile.php?form_id=main_adgroup',700,900);
				}
				else if(val.indexOf('-')>=0)
				{
					val = val.split('-');
					netWorkId	=	val[0];
					adType		=	val[1];
					unit		=	val[2];
					currentGroup = jQuery(this).parents('tr.a-group');
					//update form Info 
					currentGroup.find('span.ad-type-value').text(adType);//update ad type
					currentGroup.find('span.unit-value').text(unit);// update unit type						
					if(netWorkId==<?php echo iPhoneAppID;?>||netWorkId==<?php echo iPhoneMapID;?>||netWorkId==<?php echo iPhoneWebID;?>)
							currentGroup.find('.rCountry').show();
						else
							currentGroup.find('.rCountry').hide();
				}
			});
			//validate submitting
			jQuery('input.validateSubmit').click(function(){				
				var validate=true;
				var msg='';
				jQuery('input[name=add_gr_name[]]').each(function(){
					val = jQuery.trim(jQuery(this).val());
					if(val=='')
					{
						validate=false;
						msg+='Ad Group Name is missing\n';
						return false;
					}
				});
				jQuery('input[name=daily_budget[]]').each(function(){
					val = jQuery.trim(jQuery(this).val());
					if(val=='')
					{
						validate=false;
						msg+='Daily Budget is missing\n';
						return false;
					}
					else if(!IsNumeric(val))
					{
						validate=false;
						msg+="Daily Budget must be numeric\n";
						return false;
					}
				});
				jQuery('select[name=network[]]').each(function(){
					val = jQuery.trim(jQuery(this).find('option:selected').val());
					if(val=='')
					{
						validate=false;
						msg+='Select Network\n';
						return false;
					}
				});
				
				if(!validate)
					alert(msg);
				return validate;
			});
			// display window create ad
			jQuery('.create-ad').die('click');
			jQuery('.create-ad').live('click',function(){
				currentGroup = jQuery(this).parents('tr.a-group');
				var validate=true;
				var msg='';	
				var val='';			
				val = currentGroup.find('input[name=add_gr_name[]]').val();
				if(val=='')
				{
					validate=false;
					msg+='Ad Group Name is missing\n';				
				}	
				val = currentGroup.find('input[name=daily_budget[]]').val();
				if(val=='')
				{
					validate=false;
					msg+='Daily Budget is missing\n';		
				}	
				else if(!IsNumeric(val))
				{
					validate=false;
					msg+="Daily Budget must be numeric\n";					
				}		
				val = currentGroup.find('select[name=network[]]').val();
				if(val=='')
				{
					validate=false;
					msg+='Select Network\n';			
				}
				if(!validate)
					alert(msg);
				else
				{
					//prepair for displaying window
					val=val.split('-');
					network_id=val[0];
					GB_showCenter('Enter Your Ad','../advertiser_direct_popup.php?form_id=createad&network_id='+val[0]+'&network_type='+val[1]+'&token=<?php echo $token;?>',500,800);
				}
			});
		});
	</script>